package com.example.a79273.shandianban.fragment;

import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.PopupWindow;
import android.widget.TextView;

import com.example.a79273.shandianban.R;
import com.example.a79273.shandianban.adapter.CommentAdapter;
import com.example.a79273.shandianban.adapter.EmojiAdapter;
import com.example.a79273.shandianban.application.MyApplication;
import com.example.a79273.shandianban.model.CommentBean;
import com.handmark.pulltorefresh.library.PullToRefreshBase;
import com.handmark.pulltorefresh.library.PullToRefreshListView;

import org.xutils.HttpManager;
import org.xutils.common.Callback;
import org.xutils.http.RequestParams;
import org.xutils.x;

import java.util.ArrayList;
import java.util.List;

/**
 * 有料页面的评价
 */
public class CommentFragment extends Fragment {

    private PullToRefreshListView mRefreshListView;
    private ListView listView;
    private TextView emptyView;
    private ImageView emoji_iv;

    private List<CommentBean.DataBean.ListBean> listData;
    private CommentAdapter listAdapter;

    private HttpManager httpManager = x.http();
    private String url;
    private RequestParams requestParams;

    //TODO
    private PopupWindow popupWindow;

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_comment, null);
        initView(view);
        initData();
        setData();
        setListener();
        return view;
    }

    private void setListener() {
        mRefreshListView.setOnRefreshListener(new PullToRefreshBase.OnRefreshListener2<ListView>() {
            @Override
            public void onPullDownToRefresh(PullToRefreshBase<ListView> refreshView) {
                loadData();
                mRefreshListView.setRefreshing();
            }

            @Override
            public void onPullUpToRefresh(PullToRefreshBase<ListView> refreshView) {

            }
        });
    }

    private void setData() {
        mRefreshListView.setEmptyView(emptyView);
        mRefreshListView.setAdapter(listAdapter);
        mRefreshListView.setMode(PullToRefreshBase.Mode.PULL_FROM_START);
    }

    private void initData() {
        initPopupWindow();
        Bundle bundle = getArguments();
        if (bundle != null) {
            url = bundle.getString("url");
        }
        listData = new ArrayList<>();
        listAdapter = new CommentAdapter(getActivity(), listData);
    }

    private void initPopupWindow() {
        View view = LayoutInflater.from(getActivity()).inflate(R.layout.popupwindow, null);
        GridView gridView = (GridView) view.findViewById(R.id.gv_popupWindow_contentView);
        List<ImageView> emoji = new ArrayList<>();
        ImageView imageView;
        String resourceName;
        int resourceId;
        for (int i = 0; i < 21; i++) {
            imageView = new ImageView(getActivity());

            if (i / 10 == 0) {
                resourceName = "f00" + i;
            } else if (i / 100 == 0) {
                resourceName = "f0" + i;
            } else {
                resourceName = "f" + i;
            }
            resourceId = getActivity().getResources().getIdentifier(resourceName, "mipmap", "com.example.a79273.shandianban");
            imageView.setImageResource(resourceId);
        }
        EmojiAdapter adapter = new EmojiAdapter(emoji, getActivity());
        gridView.setAdapter(adapter);
        popupWindow = new PopupWindow(view, 500,300);
        popupWindow.setBackgroundDrawable(new ColorDrawable(0));
        popupWindow.showAsDropDown(emoji_iv);

    }

    private void initView(View view) {
        mRefreshListView = (PullToRefreshListView) view.findViewById(R.id.ptrlv_comment_comment);
        listView = mRefreshListView.getRefreshableView();
        emptyView = (TextView) view.findViewById(R.id.tv_comment_empty);
        emoji_iv = (ImageView) view.findViewById(R.id.iv_comment_emoji);
    }

    public void loadData() {
        listData.clear();
        requestParams = new RequestParams(url);
        httpManager.get(requestParams, new Callback.CommonCallback<String>() {
            @Override
            public void onSuccess(String result) {
                CommentBean bean = CommentBean.objectFromData(MyApplication.dealWithString(result));
                if (bean != null) {
                    listData.addAll(bean.getData().getList());
                    listAdapter.notifyDataSetChanged();
                    if (mRefreshListView.isRefreshing()) {
                        mRefreshListView.onRefreshComplete();
                    }
                }
            }

            @Override
            public void onError(Throwable ex, boolean isOnCallback) {

            }

            @Override
            public void onCancelled(CancelledException cex) {

            }

            @Override
            public void onFinished() {

            }
        });
    }
}
